/**
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 */
package com.jeesite.modules.storage.service;

import com.jeesite.common.entity.Page;
import com.jeesite.common.lang.StringUtils;
import com.jeesite.common.service.CrudService;
import com.jeesite.modules.storage.dao.BannerDao;
import com.jeesite.modules.storage.dao.GoodsSpecDao;
import com.jeesite.modules.storage.entity.Activity;
import com.jeesite.modules.storage.entity.Banner;
import com.jeesite.modules.storage.entity.PlateItem;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 轮播图Service
 */
@Service
@Transactional(readOnly=true)
public class BannerService extends CrudService<BannerDao, Banner> {

	@Autowired
	private ActivityService activityService;

	@Autowired
	private RouteService routeService;

	/*
	 * 处理联动
	 * */
	void handleCascade(Banner entity){
		if(entity == null)
		{
			return;
		}
		entity.setActivity(StringUtils.isBlank(entity.getActivityId()) ? null : activityService.get(entity.getActivityId()));
		entity.setRoute(StringUtils.isBlank(entity.getRouteId()) ? null : routeService.get(entity.getRouteId()));
	}

	/**
	 * 获取单条数据
	 * @param banner
	 * @return
	 */
	@Override
	public Banner get(Banner banner) {
		Banner entity = super.get(banner);
		handleCascade(entity);
		return entity;
	}
	
	/**
	 * 查询分页数据
	 * @param banner 查询条件
	 * @param banner.page 分页对象
	 * @return
	 */
	@Override
	public Page<Banner> findPage(Banner banner) {
		if(StringUtils.isBlank(banner.getOrderBy())){
			banner.setOrderBy(" sort ASC");
		}
		Page<Banner> bannerPage = super.findPage(banner);
		for (Banner item : bannerPage.getList()){
			handleCascade(item);
		}
		return  bannerPage;
	}
	
	/**
	 * 保存数据（插入或更新）
	 * @param banner
	 */
	@Override
	@Transactional(readOnly=false)
	public void save(Banner banner) {
		banner.setActivityId(banner.getType().equals("活动")?banner.getActivityId():null);
		banner.setRouteId(banner.getType().equals("栏目")?banner.getRouteId():null);
		super.save(banner);
	}
	
	/**
	 * 更新状态
	 * @param banner
	 */
	@Override
	@Transactional(readOnly=false)
	public void updateStatus(Banner banner) {
		super.updateStatus(banner);
	}
	
	/**
	 * 删除数据
	 * @param banner
	 */
	@Override
	@Transactional(readOnly=false)
	public void delete(Banner banner) {
		super.delete(banner);
	}

	/*
	* 获取推荐列表
	* */
	public List<Map> getRecommends(){
		List<Banner> banners = findList(new Banner());
		List<Map> bannerMaps = new ArrayList<>();
		for (Banner banner : banners){
			Map bannerMap = new HashMap();
			bannerMap.put("thumbnail",banner.getThumbnail());
			bannerMap.put("type",banner.getType());
			bannerMap.put("activityId",banner.getActivityId());
			bannerMap.put("routeId",banner.getRouteId());
			bannerMaps.add(bannerMap);
		}
		return bannerMaps;
	}
}